/* 
 * This Activity would be the landing activity of the app 						
 * This activity will determine whether the user has logined or not				
 * If fail to login, redirect to LoginActivity.java for login/register account	
 * If successful login, redirect to DiscoverPageActivity.java
 */

package com.comp3111.happymeal;


import java.util.List;

import com.configuration.Configuration;
import com.configuration.Owner;
import com.controller.UserFetch;
import com.object.User;
import com.user.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
	
	
public class LoadingScreenActivity extends Activity {

	 private boolean isConnected;
	 private AlertDialog.Builder dialog;
	 
	/**
	 * @return the isConnected
	 */
	public boolean getIsConnected() {
		return isConnected;
	}


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Jump to loading screen
		setContentView(R.layout.loading_screen);
		
		if(Configuration.showLog){
			Log.d("Activity","LoadingScreenActivity.java");
		}
			
		// Check if connected to Internet
		isConnected = Helper.checkInternetConnection(getApplicationContext());
		Log.d("Activity","CheckingConnectivity");

		if(!isConnected){
			// Ask for Internet connection
			dialog = new AlertDialog.Builder(this);
			dialog.setTitle("Error");
			//dialog.setIcon(android.R.drawable.ic_dialog_alert);
			dialog.setMessage("Please connect to the internet before starting this application.");
			dialog.setCancelable(false);
			dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {  
			    public void onClick(DialogInterface dialog, int which) {  
			    	System.exit(1); //Force to exit
			    }  
			});
			dialog.show();
		}
		
		/*
		 *  Check if user has logged in before
		 *  If not successful login, jump to LoginActivity.java
		 *  If successful login, jump to DiscoverPageActivity.java
		 */
		
		LoginRegister login = new LoginRegister();
		boolean isLoggedIn = login.isUserLoggedIn(getApplicationContext(),true);
		
		if(!isLoggedIn && !Configuration.skipLogin){
			//Start an intent
//			Intent intentPass = new Intent(LoadingScreenActivity.this, LoginActivity.class); //bypass switch

			Intent intentPass = new Intent(LoadingScreenActivity.this, LoginActivity.class); //bypass switch
			//Pass pass intent
			//intentPass.putExtra("USER_NAME",et.getText().toString().trim());

			//Go to next page
			startActivity(intentPass);
			finish();
		}else{
			//Start an intent
			Intent intentPass = new Intent(LoadingScreenActivity.this, DiscoverPageActivity.class);
			
			if(Configuration.skipLogin){
				//Demo info into Owner class
				Owner.id = 39;
				Owner.telephone = "51154105";
				Owner.imei = LoginRegister.getImei(getApplicationContext());
				Owner.name = "Wilson Liu";
				Owner.description = "Wilson Liu Description";
				Owner.profile_picture = "";
			}else{
				//Fetch real data into demo class
				DatabaseHandler db = new DatabaseHandler(getApplicationContext());
				List<Contact> contactList = db.getAllContacts();
				for(Contact contact:contactList){
					String telephone = contact.getTelephone();
					UserFetch userFetch = new UserFetch();
					User user = userFetch.fetchUserByTelephone(telephone);
					Owner.id = user.getId();
					Owner.telephone = telephone;
					Owner.imei = LoginRegister.getImei(getApplicationContext());
					Owner.name = user.getName();
					Owner.description = user.getDescription();
					Owner.profile_picture = user.getProfilePicture();
				}
	
			}
			
			//Go to next page
			startActivity(intentPass);
			finish();
		}

	}
	
	/*
	 * RequestInternetDialog will prompt a dialog to ask for connection
	 * to the Internet
	 */
//	private void RequestInternetDialog(){
//		//TODO: This part will be implemented
//	}
}
